﻿using SDATA;
using SDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0072 : Form
    {
        bool _isNew = true;
        string sql = "";
        string _EmployeeID;

        public frm0072()
        {
            InitializeComponent();
        }

        public frm0072(bool isNew = true, bool isView=false, string EmployeeID = "")
        {
            InitializeComponent();
            _isNew = isNew;
            btnSave.Visible = !isView;
            txtEmployeeID.Enabled = isNew;
            btnAdd.Visible = isNew;
            _isNew = isNew;
            _EmployeeID = EmployeeID;
        }

        private void frm0072_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            txtEmployeeID.Enabled = true;
            _isNew = true;
            foreach (Control c in this.Controls)
                if (c.GetType() == typeof(TextBox))
                    c.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            if (!_isNew)
            {
                UpdateInfo();
            }
            else
            {
                AddNew();
            }
        }

        private bool CheckInput()
        {
            if (txtEmployeeID.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Mã nhân viên!");
                txtEmployeeID.Focus();
                return false;
            }
            else if (txtFullName.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Tên nhân viên!");
                txtFullName.Focus();
                return false;
            }
            return true;
        }

        private void AddNew()
        {
            sql = "INSERT tb0040(EmployeeID,FullName,DepartmentID,DivisionID,";
            sql += "EmployeeTypeID,HireDate,EndDate,BirthDay,Address,";
            sql += "Tel,Fax,Email,IsUserID,Disabled,";
            sql += "CreateDate,CreateUserID,LastModifyDate,LastModifyUserID) VALUES(";
            sql += string.Format("'{0}',N'{1}','{2}','{3}',", txtEmployeeID.Text, txtFullName.Text, TDBCboDepartmentID.Text, FRM.DivisionID);
            sql += string.Format("'{0}','{1}','{2}','{3}',N'{4}',", TDBCboEmployeeTypeID.Text, DTPHireDate.Value, DTPEndDate.Value, DTPBirthDay.Value, txtAddress.Text);
            sql += string.Format("'{0}','{1}','{2}','{3}','{4}',", txtTel.Text, txtFax.Text, txtEmail.Text, chkIsUserID.Checked ? 1 : 0, chkDisabled.Checked?1:0);
            sql += string.Format("GETDATE(),'{0}',GETDATE(),'{0}')", FRM.UserName);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Lưu dữ liệu thành công!");
                (Application.OpenForms["frm0063"] as frm0063).loadDATA();
                btnSave.Enabled = false;
                txtEmployeeID.Enabled = false;
            }
            else
            {
                SDLL.DLL02.Alert("Lưu dữ liệu thất bại!");
            }
        }

        private void UpdateInfo()
        {
            sql = string.Format("UPDATE tb0040 SET FullName =N'{0}', DepartmentID='{1}',EmployeeTypeID='{2}',",txtFullName.Text,TDBCboDepartmentID.Text,TDBCboEmployeeTypeID.Text);
            sql += string.Format("HireDate='{0}',EndDate='{1}',BirthDay='{2}',Address=N'{3}',", DTPHireDate.Value, DTPEndDate.Value, DTPBirthDay.Value, txtAddress.Text);
            sql += string.Format("Tel='{0}',Fax='{1}',Email='{2}',IsUserID='{3}',Disabled='{4}',", txtTel.Text, txtFax.Text, txtEmail.Text, chkIsUserID.Checked?1:0, chkDisabled.Checked?1:0);
            sql += string.Format("LastModifyUserID='{0}', LastModifyDate=GETDATE() WHERE EmployeeID='{1}'", FRM.UserName, _EmployeeID);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frm0063"] as frm0063).loadDATA();
                Close();
            }
            else
            {
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
            }
        }

        private void frm0072_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            if (!_isNew)
            {
                sql = string.Format("SELECT * FROM tb0040 WHERE EmployeeID='{0}' AND DivisionID='{1}'", _EmployeeID, FRM.DivisionID);
                DataTable dt = FRM.GetDataTable(sql);
                if (dt.Rows.Count < 1) { return; }
                txtEmployeeID.Text = dt.Rows[0]["EmployeeID"].ToString();
                txtFullName.Text = dt.Rows[0]["FullName"].ToString();
                TDBCboDepartmentID.Text = dt.Rows[0]["DepartmentID"].ToString();
                TDBCboDepartmentID_SelectedIndexChanged(null, null);
                TDBCboEmployeeTypeID.Text = dt.Rows[0]["EmployeeTypeID"].ToString();
                TDBCboEmployeeTypeID_SelectedIndexChanged(null, null);
                txtAddress.Text = dt.Rows[0]["Address"].ToString();
                txtTel.Text = dt.Rows[0]["Tel"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtFax.Text = dt.Rows[0]["Fax"].ToString();
                chkIsUserID.Checked = dt.Rows[0]["IsUserID"].ToString()=="1"?true:false;
                DTPHireDate.Value = DateTime.Parse(dt.Rows[0]["HireDate"].ToString());
                DTPEndDate.Value = DateTime.Parse(dt.Rows[0]["EndDate"].ToString());
                DTPBirthDay.Value = DateTime.Parse(dt.Rows[0]["BirthDay"].ToString());

                //TDBCboCurrencyID.Text = dt.Rows[0]["CurrencyID"].ToString();
                //txtPeriodNum.Text = dt.Rows[0]["PeriodNum"].ToString();
                //TDBCboBranchID.Text = dt.Rows[0]["BranchID"].ToString();
                chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString() == "1" ? true : false;
            }   
        }

        private void loadDATA_Combo()
        {
            // Phòng ban
            sql = string.Format("SELECT DepartmentID,DepartmentName FROM tb0039 WHERE DivisionID='{0}' ORDER BY DepartmentID", FRM.DivisionID);
            TDBCboDepartmentID.DataSource = FRM.GetDataTable(sql);
            TDBCboDepartmentID_SelectedIndexChanged(null, null);

            // Loại nhân viên
            sql = "SELECT EmployeeTypeID,EmployeeName FROM tb0041 ORDER BY EmployeeTypeID";
            TDBCboEmployeeTypeID.DataSource = FRM.GetDataTable(sql);
            TDBCboEmployeeTypeID_SelectedIndexChanged(null, null);
        }

        private void TDBCboDepartmentID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDepartmentName.Text = TDBCboDepartmentID.SelectedItem!=null?TDBCboDepartmentID.SelectedItem.ItemData[1].ToString():"";
        }

        private void TDBCboEmployeeTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtEmployeeTypeName.Text = TDBCboEmployeeTypeID.SelectedItem != null ? TDBCboEmployeeTypeID.SelectedItem.ItemData[1].ToString() : "";

        }


    }
}
